//////////Change the directory before running the code
////////// First-stage regression
cd "D:\data&code"
use pseudo_dataset/targeting, clear

//// Multinomial logit
mlogit contest    		///
	logmv 				///
	q         			///
	roa       			///
	leverage  			///
	divyld    			///
	hhi           		///
	inst_13f			///
	log_port_val_bar 	///	
	port_share_bar 		///
	horizon_bar 		///
	basis_ret_bar 		///
	stance_bar 			///
	i.proxy_year   		///
	i.ff48        		///
	if contest == 0 | (contest > 0 & contest_year == 1), iter(25)
	
outreg2 using table_10,  						///
        dta 									///
		tstat 									///
		addstat(Pseudo R-squared, `e(r2_p)') 	///
		bdec(3) 								///
		tdec(2)  								///
		bracket 								///
		nocons 									///
		nonotes 								///
		label 									///
		keep ( 									///
			logmv              					///
			q_CCM       	        			///
			roa_CCM                 			///
			leverage_CCM            			///
			divyld_CCM              			///
			hhi 								///
			inst_13f							///
			log_port_val_bar 					///
			port_share_bar						///	
			horizon_bar 						///
			basis_ret_bar 						///
			stance_bar 							///
		)               						/// 
		replace		
				
//// Lee correction	   
predict prob_voted, outcome(1)
keep if contest == 1 & contest_year == 1
gen J = invnormal(prob_voted)
gen lee_numerator   = normalden(J)
gen lee_denominator = prob_voted
gen lee = -lee_numerator/lee_denominator

drop if lee == .
keep pf_no lee

label variable lee "LEE"
tempfile ivm_voted
save `ivm_voted', replace
							
//////// Second stage
use pseudo_dataset/voting, clear


//// Merge Lee correction
merge m:1 pf_no using `ivm_voted'
drop if _merge == 2
drop _merge

label variable lee "Lee correction term"

// Second-stage
reghdfe vote_num   						///
		logmv              				///
		q_CCM       	        		///
		roa_CCM                 		///
		leverage_CCM            		///
		divyld_CCM              		///
		hhi 							///
		inst_13f 						///
		hf  							///
		no_camp 						///
		share_intensity 				///
		log_port_val					///	
		port_share						///		
		horizon 						///
		basis_ret               		///
		lee  							///
		if contest == 1 				///
		, 								///
		absorb(proxy_year ff48 fund_id) ///
		vce(cluster fund_family)
	
//// Append to table
gen log_port_val_bar 	= .
gen port_share_bar 		= .
gen horizon_bar 		= .
gen basis_ret_bar 		= .
gen stance_bar 			= .

label variable log_port_val_bar "Log(fund assets) (firm-level)"
label variable port_share_bar   "Inv. as % of fund assets (firm-level)"
label variable horizon_bar      "Holding horizon (year) (firm-level)"
label variable basis_ret_bar    "Basis-adjusted return (firm-level)"
label variable stance_bar    	"Fund stance measure (firm-level)"
	
outreg2 using table_10,  						///
        dta 									///
		tstat 									///
		adjr2					 				///
		bdec(3) 								///
		tdec(2)  								///
		bracket 								///
		nocons 									///
		nonotes 								///
		label 									///
		keep ( 									///
			logmv              					///
			q_CCM       	        			///
			roa_CCM                 			///
			leverage_CCM            			///
			divyld_CCM              			///
			hhi 								///
			inst_13f 							///
			hf  								///
			no_camp 							///
			share_intensity 					///
			log_port_val	 					///
			port_share							///	
			horizon		 						///
			basis_ret	 						/// 
			lee 								///
		)               						/// 
		sortvar ( 								///
			logmv              					///
			q_CCM       	        			///
			roa_CCM                 			///
			leverage_CCM            			///
			divyld_CCM              			///
			hhi 								///
			inst_13f 							///
			log_port_val_bar 					///
			port_share_bar						///	
			horizon_bar 						///
			basis_ret_bar 						///
			stance_bar 							///
			) 									///
		append	

///////
use table_10_dta, clear
rm table_10.txt 
browse
